// pages/my/logon/customer/customer.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    registerInfo: {},
    loginName: '',
    pwd: '',
    phoneNumber: '',
    verificationCode: '',
    type: 0,
    forbidden: false,
    buttonText: '获取验证码',
    Type: 'primary'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      this.setData({
        type: options.type
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindModelChange: function (e) {
    wx.$$apply(e, this.data.registerInfo);
  },
  //注册
  Register: function () {
    let _this=this;
    var info = this.data.registerInfo
    var _vCode = info.verificationCode;
    if (_vCode != this.data.verificationCode) {
      wx.showModal({
        title: '提示',
        content: '请填写正确的验证码',
      })
      return
    }
    if (!info.loginName)
    {
      wx.showModal({
        title: '提示',
        content: '请输入登录名',
      })
      return
    }
    if (!info.pwd) {
      wx.showModal({
        title: '提示',
        content: '密码',
      })
      return
    }
    if (!this.validatemobile(info.Modile)) return;
    var _type = this.data.type;
    if (_type == "1") 
    {
      //登录账号验证
      LApp._get({
        url:'nbapi/auth/postname',
        data: { loginName: info.loginName},
        success:function(res){

          _this.TipToCompany(_this.data.registerInfo);
        },
        fail:function(ex){
          wx.showModal({
            title: '提示',
            content: ex,
          })
        }
      })
     
     }else if (_type == "2")
      {
      _this.Customerinfo(_this.data.registerInfo);
      }
  },
  TipToCompany: function (model){
    if (model) {
      LApp.pages.add(this);
      wx.navigateTo({
        url: '../company/company',
      })
    } 

  },
  Customerinfo: function (model) {
    LApp._post({
      url: 'nbapi/auth/customerinfo',
      data: model,
      success: function (res) {
        if (res) {
          var _url = '../loginEntrance/loginEntrance?_loginName=' + res.loginName + '&_pwd=' + res.pwd+'_status=true';
          wx.navigateTo({
            url: _url,
          })
        }
      }
    })
  },
  GetVerificationCode: function (e) {
    let _this = this;
    var phoneNumber = this.data.registerInfo.Modile;
   
    if (!this.validatemobile(phoneNumber))return;
    _this.time();
    var name = phoneNumber;
    LApp._get({
      url: 'nbapi/auth/getverificationcode',
      data: { phoneNumber: phoneNumber, name: name},
      success: function (res) {
        if (res) {
          _this.setData({
            verificationCode: res
          })
        }
      },
      fail: function (ex) {
        wx.showModal({
          title: '提示',
          content: ex,
        })
      }
    })
  },
  //验证手机号
  validatemobile: function (mobile) {
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    return true;
  },
  //倒计时
  time: function () {
    var t = 60;

    var that = this;
    function TimeCount() {
      t--;
      if (t <= 0) {
        that.setData({
          forbidden: false,
          buttonText: '获取验证码',
          Type: 'primary'
        })
      }
      else {
        that.setData({
          forbidden: true,
          buttonText: '重新发送（' + t + '）',
          Type: 'default'
        })
        setTimeout(TimeCount, 1000);
      }

    }
    setTimeout(TimeCount, 100);


  },
})